package com.lnw.service;

import com.lnw.dao.PlaceDAO;
import com.lnw.entity.Place;
import com.lnw.entity.Province;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PlaceServiceImpl implements PlaceService{
    @Autowired
    private PlaceDAO placeDAO;

    @Autowired
    private ProvinceService provinceService;

    @Override
    public List<Place> findByProvinceIdPage(Integer page, Integer rows, String provinceId) {
        int start = (page-1)*rows;
        return placeDAO.findByProvinceIdPage(start,rows,provinceId);
    }

    @Override
    public Integer findByProvinceIdCounts(String provinceId) {
        return placeDAO.findByProvinceIdCounts(provinceId);
    }

    /**
     * 保存景点
     * @param place
     */
    @Override
    public void save(Place place) {
        placeDAO.save(place);
        //保存完景点，省份下的景点+1
        Province oneProvince = provinceService.findOneProvince(place.getProvinceid());
        //更新省份信息的景点个数
        oneProvince.setPlacecounts(oneProvince.getPlacecounts()+1);
        provinceService.update(oneProvince);
    }

    /**
     * 删除景点
     * @param id
     */
    @Override
    public void delete(String id) {
        //查景点详细信息
        Place place = placeDAO.findOne(id);
        //更新省份景点个数
        Province province = provinceService.findOneProvince(place.getProvinceid());
        Province newProvince = province.setPlacecounts(province.getPlacecounts() - 1);
        provinceService.update(newProvince);

        //删除景点
        placeDAO.delete(id);

    }

    @Override
    public Place findOnePlace(String id) {
        return placeDAO.findOne(id);
    }

    @Override
    public void update(Place place) {
        placeDAO.update(place);
    }


}
